package com.team.recruitment_management_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team.recruitment_management_system.mapper.BusinessInformationMapper;
import com.team.recruitment_management_system.mapper.CompanyMapper;
import com.team.recruitment_management_system.pojo.dto.company.AddCompanyDto;
import com.team.recruitment_management_system.pojo.dto.company.ChangeCompanyDto;
import com.team.recruitment_management_system.pojo.dto.company.CompanyPageQuery;
import com.team.recruitment_management_system.pojo.entity.Company;
import com.team.recruitment_management_system.pojo.vo.CompanyInfoVo;
import com.team.recruitment_management_system.service.CompanyService;
import com.team.recruitment_management_system.util.result.PageResult;
import com.team.recruitment_management_system.util.result.Result;
import com.team.recruitment_management_system.util.result.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 86147
* @description 针对表【company(公司表)】的数据库操作Service实现
* @createDate 2024-03-21 20:28:40
*/
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService{

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private BusinessInformationMapper businessInformationMapper;
    @Override
    public Result<Integer> addCompany(AddCompanyDto addCompanyDto) {
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getName,addCompanyDto.getName());
        if(this.exists(queryWrapper)){
            return Result.build(null,500,"公司名重复");
        }

        Company company = new Company();
        BeanUtils.copyProperties(addCompanyDto,company);
        String otherTag = String.join(",", addCompanyDto.getOtherTag());
        company.setOtherTag(otherTag);
        this.save(company);
        //获取添加的公司的id并返回
//        LambdaQueryWrapper<Company> queryWrapper1 = new LambdaQueryWrapper<>();
//        queryWrapper1.eq(Company::getName, company.getName());
//        List<Company> companies = companyMapper.selectList(queryWrapper);
        Integer companyId = company.getId();
        return Result.postOk(companyId);
    }

    @Override
    public Result<Void> changeCompany(ChangeCompanyDto changeCompanyDto) {
        Company company = this.getById(changeCompanyDto.getCId());
        if(company == null){
            return Result.build(null, ResultCodeEnum.COMPANY_ERROR);
        }
        BeanUtils.copyProperties(changeCompanyDto,company);
        String otherTag = String.join(",", changeCompanyDto.getOtherTag());
        company.setOtherTag(otherTag);
        this.updateById(company);
        return Result.putOk(null);
    }

    @Override
    public Result<Void> deleteCompany(int companyId) {
        Company company = this.getById(companyId);
        if(company == null){
            return Result.build(null, ResultCodeEnum.COMPANY_ERROR);
        }
        this.removeById(company);
        return Result.deleteOk(null);
    }

    @Override
    public Result<CompanyInfoVo> getCompanyInfoByID(int companyId) {
        Company company = this.getById(companyId);
        if(company == null){
            return Result.build(null, ResultCodeEnum.COMPANY_ERROR);
        }
        CompanyInfoVo companyInfoVo = companyMapper.getCompanyInfoById(companyId);
        companyInfoVo.setBusinessInformationVo(businessInformationMapper.getBusinessInformationById(companyId));
        new Thread(()->{
            companyMapper.updateViewNum(companyId);
        }).start();
        return Result.ok(companyInfoVo);
    }

    @Override
    public PageResult<CompanyInfoVo> getCompanyPage(CompanyPageQuery companyPageQuery) {
        Page<Company> companyPage = new Page<>(companyPageQuery.getPageNum(), companyPageQuery.getPageSize());
        IPage<CompanyInfoVo> companyInfoVo = companyMapper.getCompanyPage(companyPage, companyPageQuery);
        return PageResult.success(companyInfoVo);
    }

    @Override
    public Result<List<CompanyInfoVo>> getHotCompany() {
        List<CompanyInfoVo>  companyInfoVos = new ArrayList<>();
        companyInfoVos = companyMapper.getHotCompany();
        return Result.ok(companyInfoVos);
    }
}




